

package com.lglbc;

import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Date;


@RestController
public class HomeController {
	@Autowired
	private StorageService storageService;
	@Autowired
	private OrderService orderService;
	@GetMapping(value = "/buy/{userId}")
	@GlobalTransactional(timeoutMills = 300000, name = "spring-cloud-demo-tx")
	public String account(@PathVariable("userId") String userId, @RequestParam("code") String code, @RequestParam("count") int count) throws InterruptedException {
		boolean storageResult = storageService.storage(code, count);
		if (!storageResult) {
			return "库存扣减失败";
		}
		boolean orderResult = orderService.order(userId, code, count);
		if (orderResult) {
			return "下单成功";
		}
		Thread.sleep(10000);
		throw new RuntimeException("下单失败,所有数据回滚...");
	}
}
